/* AdditionCheck.java
 * 6 June 2012
 */
package za.ac.wits.elen7045.integrity.checks;

import jass.modern.Post;
import jass.modern.Pre;

import java.util.ArrayList;
import java.util.List;

import za.ac.wits.elen7045.integrity.checks.interfaces.IntegrityCheck;
import za.ac.wits.elen7045.integrity.checks.interfaces.IntegrityCheckResult;
import za.ac.wits.elen7045.scraper.interfaces.ScraperSessionInterface;

/**
 * Implementation of a IntegrityCheck that checks the basic statement addition.
 *    Opening balance
 *  + Payment Received
 *  - New Charges
 *  - Discount
 *  - Deductions
 *  -------------------
 *    Total Due
 *  -------------------
 * @author visagieg
 *
 */
public class AdditionCheck implements IntegrityCheck {

	/**
	 * Does a basic calculation to see if the values correspond to the total
	 * Preconditions:
	 * scraperSession data pairs must contain a including a Opening balance value
	 * scraperSession data pairs must contain a including a Payment Received value
	 * scraperSession data pairs must contain a including a New Charges value
	 * scraperSession data pairs must contain a including a Discount value
	 * scraperSession data pairs must contain a including a Deductions value
	 * scraperSession data pairs must contain a including a Total Due value
	 * 
	 * @see za.ac.wits.elen7045.integrity.checks.interfaces.IntegrityCheck#doCheck(za.ac.wits.elen7045.scraper.interfaces.ScraperSessionInterface)
	 */
	@Override
	@Pre("scraperSession != null "+
			"&& scraperSession.get(\"Opening balance\") != null " +
			"&& scraperSession.get(\"Payment Received\") != null" +
			"&& scraperSession.get(\"New Charges\") != null" +
			"&& scraperSession.get(\"Discount\") != null" +
			"&& scraperSession.get(\"Deductions\") != null" +
			"&& scraperSession.get(\"Total Due\") != null")
	@Post("@Result == true || @Result == false")
	public StandardIntegrityCheckReport doCheck(ScraperSessionInterface scraperSession) {
		// TODO put datapair names in properties file
		double totaldue = Double.parseDouble(scraperSession.get("Opening balance").getValue())+
				Double.parseDouble(scraperSession.get("Payment Received").getValue())-
				Double.parseDouble(scraperSession.get("New Charges").getValue())-
				Double.parseDouble(scraperSession.get("Discount").getValue())-
				Double.parseDouble(scraperSession.get("Deductions").getValue());
		if(Double.parseDouble(scraperSession.get("Total Due").getValue()) == totaldue){
			List<IntegrityCheckResult> result = new ArrayList<IntegrityCheckResult>();
			result.add(new StandardIntegrityCheckResult("Total Due", scraperSession.get("Total Due").getValue(), "Total Due", totaldue+""));
			return new StandardIntegrityCheckReport(true, result);			
		}else{
			List<IntegrityCheckResult> result = new ArrayList<IntegrityCheckResult>();
			result.add(new StandardIntegrityCheckResult("Total Due", scraperSession.get("Total Due").getValue(), "Total Due", totaldue+""));
			return new StandardIntegrityCheckReport(false, result);			
		}
	}

}
